package com.xuqi.W3;

public class W3Request {
	//////////////////////////////////////////////////////////////////////////
	/// Definition of Constant(s)
	
	/** method: POST */
	public static final String W3_REQ_METHOD_POST	= "POST";
	/** method: GET */
	public static final String W3_REQ_METHOD_GET	= "GET";
	
	/** no header to set */
	public static final int W3_REQ_SEND_HEADER_FLAG_NONE		= 0;
	/** specified headers */
	public static final int W3_REQ_SEND_HEADER_FLAG_SPECIFIED	= 1;

	/** no data to send */
	public static final int W3_REQ_SEND_DATA_FLAG_NONE			= 0;
	/** buffer data to send */
	public static final int W3_REQ_SEND_DATA_FLAG_FROM_BUFFER	= 1;
	/** file data to send */
	public static final int W3_REQ_SEND_DATA_FLAG_FROM_FILE		= 2;

	/** NOT to receive data */
	public static final int W3_REQ_RECEIVE_DATA_FLAG_NONE		= 0;
	/** receive data to buffer */
	public static final int W3_REQ_RECEIVE_DATA_FLAG_TO_BUFFER	= 1;
	/** receive data to file */
	public static final int W3_REQ_RECEIVE_DATA_FLAG_TO_FILE	= 2;
	
	/** default retry count */
	public static final int W3_REQ_RETRY_COUNT_0 = 0;
	
	/// Definition of Constant(s) END
	//////////////////////////////////////////////////////////////////////////
	/// Definition of Member(s)

	private boolean	m_bNativeRequest;
	private String	m_cURL;				///< URL (supports HTTP and HTTPS)
	private String	m_cMethod;			///< HTTP method (supports POST and GET)

	private int		m_iSendHeaderFlag;	///< W3_REQ_SEND_HEADER_FLAG_*
	private String	m_cSendHeaders;

	private int		m_iSendDataFlag;	///< W3_REQ_SEND_DATA_FLAG_*
	private byte[]	m_bySendData;
	private String	m_cSendFileName;

	private int		m_iReceiveDataFlag;	///< W3_REQ_RECEIVE_DATA_FLAG_*
	private String	m_cReceiveFileName;
	
	private int 	m_iRetryCount;		///< retry count
	
	/// Definition of Member(s) END
	//////////////////////////////////////////////////////////////////////////
	/// None-static Block
	
	public W3Request()
	{
		m_bNativeRequest	= false;

		m_cURL				= null;
		m_cMethod			= null;

		m_iSendHeaderFlag	= W3_REQ_SEND_HEADER_FLAG_NONE;
		m_cSendHeaders		= null;

		m_iSendDataFlag		= W3_REQ_SEND_DATA_FLAG_NONE;
		m_bySendData		= null;
		m_cSendFileName		= null;

		m_iReceiveDataFlag	= W3_REQ_RECEIVE_DATA_FLAG_NONE;
		m_cReceiveFileName	= null;
		
		m_iRetryCount		= W3_REQ_RETRY_COUNT_0;
	}
	
/*	protected W3Request(boolean bNative) {
		this();
		m_bNativeRequest = bNative;
	}*/
	
	/// None-static Block END
	//////////////////////////////////////////////////////////////////////////
	/// Function(s) 

	/**
	 * Whether it's a request from native code.
	 */
/*	public boolean isNativeRequest() {
		return m_bNativeRequest;
	}*/

	/**
	 * Get the URL
	 * @return	the URL
	 */
	public String getURL() {
		return m_cURL;
	}

	/**
	 * Set the URL
	 * @param cURL	URL
	 */
	public void setURL(String cURL) {
		m_cURL = cURL;
	}

	/**
	 * Get the Method
	 * @return	the method
	 */
	public String getMethod() {
		return m_cMethod;
	}

	/**
	 * Set the method
	 * @param cMethod	method
	 */
	public void setMethod(String cMethod) {
		m_cMethod = cMethod;
	}

	/**
	 * Get the flag of sending header 
	 * @return	the flag of sending header
	 */
	public int getSendHeaderFlag() {
		return m_iSendHeaderFlag;
	}

	/**
	 * Set the flag of sending flag
	 */
	protected void setSendHeaderFlag(int iSendHeaderFlag) {
		m_iSendHeaderFlag = iSendHeaderFlag;
	}

	/**
	 * Get the Headers String
	 * 
	 * @note string format: "<field>:<value>\r\n..."
	 * @return	the string of headers
	 */
	public String getSendHeaders() {
		return m_cSendHeaders;
	}

	/**
	 * Set the Headers String
	 * 
	 * @note string format: "<field>:<value>\r\n..."
	 * @param cSendHeaders	the string of headers
	 */
	public void setSendHeaders(String cSendHeaders) {
		if ((null == cSendHeaders) || (cSendHeaders.length() <= 0)) {
			m_cSendHeaders = null;
			setSendHeaderFlag(W3Request.W3_REQ_SEND_HEADER_FLAG_NONE);
			return;
		}

		m_cSendHeaders = cSendHeaders;
		setSendHeaderFlag(W3Request.W3_REQ_SEND_HEADER_FLAG_SPECIFIED);
	}

	/**
	 * Get the flag of sending data
	 * @return	the flag of sending data
	 */
	public int getSendDataFlag() {
		return m_iSendDataFlag;
	}

	/**
	 * Set the flag of sending data
	 * @param iSendDataFlag	the flag of sending data
	 */
	protected void setSendDataFlag(int iSendDataFlag) {
		m_iSendDataFlag = iSendDataFlag;
	}

	/**
	 * Get sending data
	 * @return	the sending data
	 */
	public byte[] getSendData() {
		return m_bySendData;
	}

	/**
	 * Set the sending data
	 * @param bySendData
	 */
	public void setSendData(byte[] bySendData) {
		if ((null == bySendData) || (bySendData.length <= 0)) {
			m_bySendData = null;
			setSendDataFlag(W3Request.W3_REQ_SEND_DATA_FLAG_NONE);
			return;
		}

		m_bySendData = bySendData;
		setSendDataFlag(W3Request.W3_REQ_SEND_DATA_FLAG_FROM_BUFFER);
	}

	/**
	 * Get sending file name
	 * @return	the name of sending file
	 */
	public String getSendFileName() {
		return m_cSendFileName;
	}

	/**
	 * Set the sending file name
	 * @param cSendFileName
	 */
	public void setSendFileName(String cSendFileName) {
		if ((null == cSendFileName) || (cSendFileName.length() <= 0)) {
			m_cSendFileName = null;
			setSendDataFlag(W3Request.W3_REQ_SEND_DATA_FLAG_NONE);
			return;
		}

		m_cSendFileName = cSendFileName;
		setSendDataFlag(W3Request.W3_REQ_SEND_DATA_FLAG_FROM_FILE);
	}

	/**
	 * Get the flag of receiving data
	 * @return	the flag of receiving data
	 */
	public int getReceiveDataFlag() {
		return m_iReceiveDataFlag;
	}

	/**
	 * Set the flag of receiving data
	 * @param iReceiveDataFlag	the flag of receiving data
	 */
	protected void setReceiveDataFlag(int iReceiveDataFlag) {
		m_iReceiveDataFlag = iReceiveDataFlag;
	}

	/**
	 * Set the flag: NOT to receive data
	 */
	public void setReceiveDataNone() {
		m_cReceiveFileName = null;
		setReceiveDataFlag(W3Request.W3_REQ_RECEIVE_DATA_FLAG_NONE);
	}

	/**
	 * Set the flag: to receive data to buffer
	 */
	public void setReceiveDataToBuffer() {
		m_cReceiveFileName = null;
		setReceiveDataFlag(W3Request.W3_REQ_RECEIVE_DATA_FLAG_TO_BUFFER);
	}

	/**
	 * Get the received filename
	 * @return	the received filename  
	 */
	public String getReceiveFileName() {
		return m_cReceiveFileName;
	}

	/**
	 * Set the received filename
	 * @param cReceiveFileName	the received filename
	 */
	public void setReceiveFileName(String cReceiveFileName) {
		if ((null == cReceiveFileName) || (cReceiveFileName.length() <= 0)) {
			m_cReceiveFileName = null;
			setReceiveDataFlag(W3Request.W3_REQ_RECEIVE_DATA_FLAG_NONE);
			return;
		}

		m_cReceiveFileName = cReceiveFileName;
		setReceiveDataFlag(W3Request.W3_REQ_RECEIVE_DATA_FLAG_TO_FILE);
	}
	
	/**
	 * Get the retry count
	 * @return return the retry count
	 */
	public int getRetryCount() {
		return m_iRetryCount;
	}
	
	/**
	 * Set the retry count
	 * @param retryCount retry count
	 */
	public void setRetryCount(int retryCount) {
		m_iRetryCount = retryCount;
	}
	
	/// Function(s) END
	//////////////////////////////////////////////////////////////////////////
}
